from util.ExpRequest import ExpRequest,Output
import util.globalvar as GlobalVar
import prettytable as pt
"""
from ClassCongregation import Dnslog
DL = Dnslog()
DL.dns_host()
DL.result()
"""
class {{service.entry_nodes.vulname}}():
    def __init__(self, **env):
        """
        基础参数初始化
        """
        self.url = env.get('url')
        self.cookie = env.get('cookie')
        self.cmd = env.get('cmd')
        self.pocname = env.get('pocname')
        self.vuln = env.get('vuln')
        self.timeout = env.get('timeout')
        self.retry_time = env.get('retry_time')
        self.retry_interval = env.get('retry_interval')
            
    def {{service.entry_nodes.cvename}}(self):
        appName = '{{service.entry_nodes.vulname}}'
        pocname = '{{service.entry_nodes.cvename}}'
        path = '{{service.header_nodes.headinfo.path}}'
        method = '{{service.header_nodes.headinfo.method}}'
        desc = '{{service.entry_nodes.infoname}} {{service.entry_nodes.banner}}'
        data = '{{service.header_nodes.content.data}}'
        headers = {{service.header_nodes.headinfo.header}}
        #输出类
        output = Output(self.url, pocname)
        #请求类
        exprequest = ExpRequest(pocname, output)
        try:
            if self.vuln == 'False':
                r = exprequest.{{service.header_nodes.headinfo.method}}(self.url+path, data=data, headers=headers, retry_time=self.retry_time, retry_interval=self.retry_interval, timeout=self.timeout, verify=False)
                {{service.entry_nodes.condition}}
                    return output.no_echo_success(method, desc)
                else:
                    return output.fail()
            else:
                result = exprequest.{{service.header_nodes.headinfo.method}}(self.url+path, data=data, headers=headers, retry_time=self.retry_time, retry_interval=self.retry_interval, timeout=self.timeout, verify=False).text
                print(result)
        except Exception as error:
            return output.error_output(str(error))

tb = pt.PrettyTable()
tb.field_names = ['Target type', 'Vuln Name', 'Impact Version && Vulnerability description']
tb.align['Target type'] = 'l'
tb.align['Vuln Name'] = 'l'
tb.align['Impact Version && Vulnerability description'] = 'l'
tb.add_row([
    '{{service.entry_nodes.vulname}}',
    '{{service.entry_nodes.cvename}}',
    '{{service.entry_nodes.infoname}} {{service.entry_nodes.banner}}'
])
print(tb)

def check(**kwargs):
    thread_list = []
    Exp{{service.entry_nodes.vulname}} = {{service.entry_nodes.vulname}}(**kwargs)
    #调用单个函数
    if kwargs['pocname'] != 'ALL':
        thread_list.append(kwargs['pool'].submit(getattr(Exp{{service.entry_nodes.vulname}}, kwargs['pocname'])))
    #调用所有函数
    else:
        for func in dir({{service.entry_nodes.vulname}}):
            if not func.startswith("__"):
                thread_list.append(kwargs['pool'].submit(getattr(Exp{{service.entry_nodes.vulname}}, func)))
    #保存全局子线程列表
    GlobalVar.add_value('thread_list', thread_list)